package client.GUI.order;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Color;
import java.io.IOException;

import client.GUI.GUIClient;
import client.GUI.GUIConstants;
import client.backend.price.PriceController;
import client.backend.user.UserInformation;
import client.GUI.dashboard.OpenOrderPanel;
import client.GUI.dashboard.PriceHistoryPanel;
import client.backend.Connection;

/**
 * Brings up the new order screen, allows the user to create a new limit order.
 */
public class LimitOrder extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;
	
	private JComboBox currPairs, buySell;
	private JTextField ratio, enterAmount, enterLimPrice;
	private JButton seePriceHistory, create, back;
	
	/**
	 * This is the default constructor
	 */
	public LimitOrder() {
		super();
		initialize();
	}
	
	/**
	 * Sets the ratio of currencies to the currencies
	 * provided in currPair
	 * 
	 * @param currPair The Pair of currencies that will be assigned to ratio
	 */
	public LimitOrder(String currPair) {
		this();

		currPairs.setSelectedItem(currPair);
		
		updateRatio();
}
	/**
	 * Brings up a create new limit order panel and keeps the data(if any)
	 * that user inputed for currency pair, buy/sell and amount that
	 * the user inputed in the create new order panel
	 * 
	 * @param currPair	The Object that the user selected in the create new order panel for currency pair 
	 * @param bS		The Object that the user selected in the create new order panel for buy/sell
	 * @param amount	The String that the user inputed in the text field representing the amount of money he wants to trade with
	 */
	public LimitOrder(Object currPair, Object bS, String amount) {
		this();

		currPairs.setSelectedItem(currPair);
		buySell.setSelectedItem(bS);
		enterAmount.setText(amount);
	}

	/**
	 * Initializes the create new order panel, creates, sizes and aligns all
	 * the components
	 */
	private void initialize() {
		//Create the JLabels
		JLabel title = new JLabel("Create New Limit Order");
		JLabel selectCurr = new JLabel("Select the currencies you wish to trade:");
		JLabel currRatio = new JLabel("Current ratio:");
		JLabel amount = new JLabel("Enter amount of money you wish to trade: ");
		JLabel limitPrice = new JLabel("Limiting Price:");
			
		//Create the drop down menus
		currPairs = new JComboBox(PriceController.getInstance().getCurrPairs());
		buySell = new JComboBox(GUIConstants.buySell);
		
		//Create the text fields
		ratio = new JTextField();
		ratio.setEditable(false);
		enterAmount = new JTextField();
		enterLimPrice = new JTextField();
		
		//Create the buttons
		seePriceHistory = new JButton("Price History");
		back = new JButton("Back");
		create = new JButton("Create Order");
		
		//Set the components' bounds
		title.setBounds(255, 50, 142, 40);
		selectCurr.setBounds(90, 190, 230, 30);
		currPairs.setBounds(370, 190, 96, 30);
		buySell.setBounds(500, 190, 50, 30);
		seePriceHistory.setBounds(600, 190, 150, 30);
		currRatio.setBounds(90, 250, 77, 30);
		ratio.setBounds(184, 250, 100, 30);
		amount.setBounds(90, 350, 240, 30);
		enterAmount.setBounds(370, 350, 50, 30);
		limitPrice.setBounds(90, 410, 240, 30);
		enterLimPrice.setBounds(370, 410, 50, 30);
		create.setBounds(90, 550, 120, 30);
		back.setBounds(370, 550, 120, 30);
		
		
		/*I reduced the size of the panel by 50 in order to make room for the bottom panel*/
		this.setSize(1024, 718);
		this.setLayout(null);
		
		//Add action listeners
		currPairs.addActionListener(this);
		buySell.addActionListener(this);
		create.addActionListener(this);
		back.addActionListener(this);
		seePriceHistory.addActionListener(this);
		
		//Set the panel's colors
		this.setBackground(Color.black);
		title.setForeground(Color.white);
		selectCurr.setForeground(Color.white);
		currRatio.setForeground(Color.white);
		amount.setForeground(Color.white);
		limitPrice.setForeground(Color.white);
		
		//Add all the components
		this.add(title);
		this.add(selectCurr);
		this.add(currPairs);
		this.add(buySell);
		this.add(seePriceHistory);
		this.add(ratio);
		this.add(currRatio);
		this.add(amount);
		this.add(enterAmount);
		this.add(limitPrice);
		this.add(enterLimPrice);
		this.add(create);
		this.add(back);
	}
	
	/**
	 * Checks for actions and performs the appropriate tasks
	 */
	public void actionPerformed(ActionEvent e) {
		//Create a new order
		if (e.getSource() == create) {
			createNewLO();
		}
		//Show the price history table for the selected currency pair
		else if (e.getSource() == seePriceHistory){
			int bS = 2;
			if(buySell.getSelectedItem() == "Buy"){
				bS = 1;
			}
			
			PriceHistoryPanel prices;
			try {
				prices = new PriceHistoryPanel(currPairs.getSelectedItem().toString(), bS);
				prices.pack();
				prices.setVisible(true);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		//Go back
		else if (e.getSource() == back) {
			GUIClient.getInstance().backFromLimitOrder();
		}
		//Update the ratio if the user changes his buy/sell selection
		else if ((e.getSource() == buySell) || (e.getSource() == currPairs)) {
			updateRatio();
		}
	}
	
	/**
	 * Updates the currency ratio to match the user's buy/sell selection
	 */
	public void updateRatio() {
		String curr1 = currPairs.getSelectedItem().toString().substring(0,3);
		String curr2 = currPairs.getSelectedItem().toString().substring(3,6);
		
		if (buySell.getSelectedItem().toString().equals("Buy")) 
			ratio.setText(PriceController.getInstance().getPrice(curr1, curr2).getBuy()+"");
		else
			ratio.setText(PriceController.getInstance().getPrice(curr1, curr2).getSell()+"");
	}
	
	/**
	 * Creates the new limit order
	 */
	public void createNewLO() {
		String currPair = currPairs.getSelectedItem().toString();
		String amount = enterAmount.getText();
		String buySell = this.buySell.getSelectedItem().toString();
		String limPrice = enterLimPrice.getText();
		

		if (UserInformation.getInstance().getUser().createLO(currPair, buySell, amount, limPrice)) {
			GUIClient.getInstance().popupLO(currPair, buySell, amount, limPrice);
		}
		else
			GUIClient.getInstance().popupFail();
		
		GUIClient.getInstance().loggedIn();
	}
}  //  @jve:decl-index=0:visual-constraint="82,5"



